package com.v9.system.controller;


import com.v9.common.dto.IdDto;
import com.v9.common.dto.PageInfoDto;
import com.v9.common.exception.BizException;
import com.v9.common.web.AbstractController;
import com.v9.system.domain.CodeTable;
import com.v9.system.domain.CodeTableType;
import com.v9.system.service.CodeTableService;
import com.v9.system.service.CodeTableTypeService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.Data;
import org.hibernate.validator.constraints.Length;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author lunshi
 */
@RestController
@Tag(name = "码表")
@RequestMapping(value = "/system/dict/data", method = RequestMethod.POST)
public class CodeTableController extends AbstractController {
    @Resource
    private CodeTableService codeTableService;

    @Resource
    private CodeTableTypeService codeTableTypeInfoService;

    @RequestMapping(value = "save")
    @Operation(summary = "保存")
    public void save(@RequestBody @Valid CodeTableDto dto) {
        CodeTable clone = clone(CodeTable.class, dto);
        CodeTableType t = new CodeTableType();
        t.setCode(dto.getCodeType());
        CodeTableType tableType = codeTableTypeInfoService.findByOne(t);
        if (tableType == null) {
            throw new BizException("码表不存在");
        }
        if (dto.getId() != null) {
            codeTableService.updateById(clone);
            return;
        }
        if (dto.getId() == null) {
            codeTableService.insert(clone);
        }
    }

    @RequestMapping("remove")
    @Operation(summary = "删除")
    public void remove(@RequestBody List<IdDto> idtos) {
        List<Long> ids = idtos.stream().map(IdDto::getId).collect(Collectors.toList());
        codeTableService.remove(false, ids.toArray(new Long[ids.size()]));
    }

    @RequestMapping("view")
    @Operation(summary = "查看")
    public CodeTableDto view(@RequestBody IdDto dto) {
        return clone(CodeTableDto.class, codeTableService.findById(dto.getId()));
    }

    @RequestMapping("list")
    @Operation(summary = "list")
    public Page<CodeTableDto> list(@RequestBody CodeTableQueryDto dto) {
        CodeTable clone = clone(CodeTable.class, dto);
        Page<CodeTable> sourcepage = codeTableService.queryByPage(clone, dto.getPageable());
        return clone(CodeTableDto.class, sourcepage);
    }

    @Data
    public static class CodeTableQueryDto extends PageInfoDto {
        @NotEmpty(message = "非法操作，codeType不能为空")
        private String codeType;
    }

    @Data
    public static final class CodeTableDto {
        private Long id;
        /**
         * 名称
         */
        @NotEmpty(message = "值不能为空")
        private String name;
        /**
         * 备注
         */
        @Length(max = 64)
        private String remark;
        /**
         * 编码ø
         */

        @NotEmpty(message = "编码不能为空")
        @Length(max = 32)
        private String code;

        /**
         * 码表类型
         */
        @NotEmpty(message = "错误的请求")
        @Length(max = 32)
        private String codeType;
    }

    @Data
    public static final class CodeTypeDto {
        @Schema(description = "码表类型")
        @NotNull
        private String codeType;
    }
}
